package com.dianping.cat.alarm.policy.transform;

import com.dianping.cat.alarm.policy.IEntity;
import com.dianping.cat.alarm.policy.entity.AlertPolicy;
import com.dianping.cat.alarm.policy.entity.Group;
import com.dianping.cat.alarm.policy.entity.Level;
import com.dianping.cat.alarm.policy.entity.Type;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;








public class DefaultSaxParser
  extends DefaultHandler
{
  private DefaultLinker m_linker = new DefaultLinker(true);

/*  34 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

  private Stack<String> m_tags = new Stack();

/*  38 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  42 */   private StringBuilder m_text = new StringBuilder();


/*  45 */   public static AlertPolicy parse(InputStream in) throws SAXException, IOException { return (AlertPolicy)parseEntity(AlertPolicy.class, new InputSource((InputStream)removeBOM(in))); }



/*  49 */   public static AlertPolicy parse(Reader reader) throws SAXException, IOException { return (AlertPolicy)parseEntity(AlertPolicy.class, new InputSource((Reader)removeBOM(reader))); }



/*  53 */   public static AlertPolicy parse(String xml) throws SAXException, IOException { return (AlertPolicy)parseEntity(AlertPolicy.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  59 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  60 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  62 */       factory.setValidating(false);
/*  63 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  65 */       factory.newSAXParser().parse(is, handler);
/*  66 */       return (T)handler.getEntity();
/*  67 */     } catch (ParserConfigurationException e) {
/*  68 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  73 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  77 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  83 */     if (obj instanceof String) {
/*  84 */       String str = (String)obj;

/*  86 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  87 */         return (T)str.substring(1);
      }
/*  89 */       return obj;
    }
/*  91 */     if (obj instanceof InputStream) {
/*  92 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  94 */       in.mark(3);

/*  96 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/*  97 */         in.reset();
      }

/* 100 */       return (T)in;
/* 101 */     }  if (obj instanceof Reader) {
/* 102 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 104 */       in.mark(1);

/* 106 */       if (in.read() != 65279) {
/* 107 */         in.reset();
      }

/* 110 */       return (T)in;
    }
/* 112 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 118 */     if (value == null || value.length() == 0) {
/* 119 */       return defaultValue;
    }

/* 122 */     if (type == Boolean.class)
/* 123 */       return (T)Boolean.valueOf(value); 
/* 124 */     if (type == Integer.class)
/* 125 */       return (T)Integer.valueOf(value); 
/* 126 */     if (type == Long.class)
/* 127 */       return (T)Long.valueOf(value); 
/* 128 */     if (type == Short.class)
/* 129 */       return (T)Short.valueOf(value); 
/* 130 */     if (type == Float.class)
/* 131 */       return (T)Float.valueOf(value); 
/* 132 */     if (type == Double.class)
/* 133 */       return (T)Double.valueOf(value); 
/* 134 */     if (type == Byte.class)
/* 135 */       return (T)Byte.valueOf(value); 
/* 136 */     if (type == Character.class) {
/* 137 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 139 */     return (T)value;
  }




/* 145 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 150 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 155 */     if (uri == null || uri.length() == 0) {
/* 156 */       this.m_objs.pop();
/* 157 */       this.m_tags.pop();
    }


/* 161 */     this.m_text.setLength(0);
  }


/* 165 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 169 */   protected String getText() { return this.m_text.toString(); }


  private void parseForAlertPolicy(AlertPolicy parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 173 */     if ("type".equals(qName)) {
/* 174 */       Type type = this.m_maker.buildType(attributes);

/* 176 */       this.m_linker.onType(parentObj, type);
/* 177 */       this.m_objs.push(type);
    } else {
/* 179 */       throw new SAXException(String.format("Element(%s) is not expected under alert-policy!", new Object[] { qName }));
    }

/* 182 */     this.m_tags.push(qName);
  }

  private void parseForGroup(Group parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 186 */     if ("level".equals(qName)) {
/* 187 */       Level level = this.m_maker.buildLevel(attributes);

/* 189 */       this.m_linker.onLevel(parentObj, level);
/* 190 */       this.m_objs.push(level);
    } else {
/* 192 */       throw new SAXException(String.format("Element(%s) is not expected under group!", new Object[] { qName }));
    }

/* 195 */     this.m_tags.push(qName);
  }

  private void parseForLevel(Level parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 199 */     this.m_objs.push(parentObj);
/* 200 */     this.m_tags.push(qName);
  }

  private void parseForType(Type parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 204 */     if ("group".equals(qName)) {
/* 205 */       Group group = this.m_maker.buildGroup(attributes);

/* 207 */       this.m_linker.onGroup(parentObj, group);
/* 208 */       this.m_objs.push(group);
    } else {
/* 210 */       throw new SAXException(String.format("Element(%s) is not expected under type!", new Object[] { qName }));
    }

/* 213 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 217 */     if ("alert-policy".equals(qName)) {
/* 218 */       AlertPolicy alertPolicy = this.m_maker.buildAlertPolicy(attributes);

/* 220 */       this.m_entity = alertPolicy;
/* 221 */       this.m_objs.push(alertPolicy);
/* 222 */       this.m_tags.push(qName);
/* 223 */     } else if ("type".equals(qName)) {
/* 224 */       Type type = this.m_maker.buildType(attributes);

/* 226 */       this.m_entity = type;
/* 227 */       this.m_objs.push(type);
/* 228 */       this.m_tags.push(qName);
/* 229 */     } else if ("group".equals(qName)) {
/* 230 */       Group group = this.m_maker.buildGroup(attributes);

/* 232 */       this.m_entity = group;
/* 233 */       this.m_objs.push(group);
/* 234 */       this.m_tags.push(qName);
/* 235 */     } else if ("level".equals(qName)) {
/* 236 */       Level level = this.m_maker.buildLevel(attributes);

/* 238 */       this.m_entity = level;
/* 239 */       this.m_objs.push(level);
/* 240 */       this.m_tags.push(qName);
    } else {
/* 242 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 248 */     if (uri == null || uri.length() == 0) {
/* 249 */       if (this.m_objs.isEmpty()) {
/* 250 */         parseRoot(qName, attributes);
      } else {
/* 252 */         Object parent = this.m_objs.peek();
/* 253 */         String tag = (String)this.m_tags.peek();

/* 255 */         if (parent instanceof AlertPolicy) {
/* 256 */           parseForAlertPolicy((AlertPolicy)parent, tag, qName, attributes);
/* 257 */         } else if (parent instanceof Type) {
/* 258 */           parseForType((Type)parent, tag, qName, attributes);
/* 259 */         } else if (parent instanceof Group) {
/* 260 */           parseForGroup((Group)parent, tag, qName, attributes);
/* 261 */         } else if (parent instanceof Level) {
/* 262 */           parseForLevel((Level)parent, tag, qName, attributes);
        } else {
/* 264 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 268 */       this.m_text.setLength(0);
    } else {
/* 270 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/policy/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */